<template>
  <div class="container">
    <el-dialog
      title="新增"
      :visible="showPanys"
      width="600px"
      center
      @close="btnCancel"
    >
      <el-form
        label-width="120px"
        :model="formDate"
        :rules="formRules"
        ref="jiaoyan"
      >
        <el-form-item label="企业名称" prop="shortName">
          <el-input style="width:80%" v-model="formDate.shortName"></el-input>
        </el-form-item>
        <el-form-item label="所属公司" prop="company">
          <el-input style="width:80%" v-model="formDate.company"></el-input>
        </el-form-item>
        <el-form-item label="城市地区" prop="province">
          <!-- <el-input style="width:85%"></el-input> -->
          <el-select
            @change="diqu"
            v-model="formDate.province"
            placeholder="请选择"
            style="width:40%"
          >
            <el-option
              v-for="(item, ind) in options"
              :key="ind"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
          <el-select
            v-model="formDate.city"
            placeholder="请选择"
            style="width:40%"
          >
            <el-option
              v-for="(item, ind) in optionsx"
              :key="ind"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="方向(企业标签)" prop="tags">
          <el-input style="width:80%" v-model="formDate.tags"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input
            style="width:80%"
            type="textarea"
            :rows="2"
            v-model="formDate.remarks"
          ></el-input>
        </el-form-item>
      </el-form>

      <!-- 底部按钮 -->
      <el-row type="flex" justify="center" slot="footer">
        <el-col :span="6">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button size="small" type="primary" @click="btnOK">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
// 导入回填数据、新增、编辑接口
import { disabled, add, update } from '@/api/hmmm/companys'
// 导入 城市、地区的方法
import { provinces, citys } from '@/api/hmmm/citys'
export default {
  props: {
    showPanys: {
      // 控制当前弹层的开关
      tyep: Boolean,
      default: false
    }
  },
  data() {
    return {
      formDate: {
        shortName: '', // 企业名称
        company: '', // 所属公司
        province: '', // 所属城市
        city: '', // 所属地区
        tags: '', // 方向 标签
        remarks: '', // 备注
        id: '' // 用户区别 编辑和新增事件
      },
      isFamous: true, // 是否为名企业
      options: [], // 存放城市数据
      optionsx: [], // 存放地区数据
      formRules: {
        // form表单的校验
        shortName: [{ required: true, message: '不能为空', trigger: 'blur' }],
        company: [{ required: true, message: '不能为空', trigger: 'blur' }],
        province: [{ required: true, message: '不能为空', trigger: 'change' }],
        city: [{ required: true, message: '不能为空', trigger: 'change' }],
        tags: [{ required: true, message: '不能为空', trigger: 'blur' }],
        remarks: [{ required: true, message: '不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.chengshi() // 调用获取城市数据
  },
  methods: {
    // 请求城市
    async chengshi() {
      this.options = await provinces()
    },
    // 请求地区
    async diqu() {
      this.formDate.city=''
      this.optionsx = await citys(this.formDate.province)
    },
    // 取消按钮，清空表单，移除校验，关闭弹层
    async btnCancel() {
      this.formDate = {
        shortName: '',
        company: '',
        province: '',
        city: '',
        tags: '',
        remarks: '',
        id: ''
      }
      await this.$refs.jiaoyan.resetFields()
      this.$emit('update:showPanys', false)
    },
    // 确定按钮
    async btnOK() {
      await this.$refs.jiaoyan.validate()
      try {
        if (this.formDate.id) {
          // 如果form 里面有id 表示编辑操作
          update({ ...this.formDate, isFamous: this.isFamous })
          this.$message({
            showClose: true,
            message: '编辑成功',
            type: 'success'
          })
        } else {
          // 没有id  执行新增操作
          add({ ...this.formDate, isFamous: this.isFamous })
          this.$message({
            showClose: true,
            message: '新增成功',
            type: 'success'
          })
        }
        this.$emit('addupdate')
      } catch (error) {
        this.$message({
          showClose: true,
          message: error,
          type: 'error'
        })
      }
    },

    // 回填数据
    async disabled(row) {
      console.log(row)
      const data = row
      this.formDate = data
    }
  }
}
</script>

<style scoped lang="less"></style>
